﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace MMD.VMD
{
	public class VMDLoader
	{
		public static VMDFormat Load(BinaryReader bin, string path, string clip_name, bool isSave)
		{
			VMDFormat vmdformat = new VMDFormat(bin, path, clip_name);
			if (isSave == false) return vmdformat;
			StringBuilder stringBuilder = new StringBuilder();
			VMDFormat.MotionList motion_list = vmdformat.motion_list;
			if (motion_list != null)
			{
				stringBuilder.Append(motion_list.motion_count);
				stringBuilder.AppendLine();
				foreach (KeyValuePair<string, List<VMDFormat.Motion>> keyValuePair in motion_list.motion)
				{
					stringBuilder.Append(keyValuePair.Key);
					stringBuilder.AppendLine();
				}
				foreach (KeyValuePair<string, List<VMDFormat.Motion>> keyValuePair2 in motion_list.motion)
				{
					List<VMDFormat.Motion> value = keyValuePair2.Value;
					for (int i = 0; i < value.Count; i++)
					{
						VMDFormat.Motion motion = value[i];
						stringBuilder.AppendFormat("{0}:{1}/{2}/{3}", new object[]
						{
								motion.bone_name,
								motion.flame_no,
								motion.location,
								motion.rotation
						});
						for (int j = 0; j < motion.interpolation.Length; j++)
						{
							stringBuilder.Append(motion.interpolation[j]);
							stringBuilder.Append(',');
						}
						stringBuilder.AppendLine();
					}
				}
				string value2 = stringBuilder.ToString();
				using (TextWriter textWriter = File.CreateText(path + ".motion.txt"))
				{
					textWriter.Write(value2);
				}
				stringBuilder.Remove(0, stringBuilder.Length);
			}
			VMDFormat.SkinList skin_list = vmdformat.skin_list;
			if (skin_list != null)
			{
				stringBuilder.Append(skin_list.skin_count);
				stringBuilder.AppendLine();
				foreach (KeyValuePair<string, List<VMDFormat.SkinData>> keyValuePair3 in skin_list.skin)
				{
					stringBuilder.Append(keyValuePair3.Key);
					stringBuilder.AppendLine();
					List<VMDFormat.SkinData> value3 = keyValuePair3.Value;
					for (int k = 0; k < value3.Count; k++)
					{
						VMDFormat.SkinData skinData = value3[k];
						stringBuilder.AppendFormat("{0}:{1}/{2}", skinData.skin_name, skinData.flame_no, skinData.weight);
						stringBuilder.AppendLine();
					}
				}
				string value4 = stringBuilder.ToString();
				using (TextWriter textWriter2 = File.CreateText(path + ".skin.txt"))
				{
					textWriter2.Write(value4);
				}
				stringBuilder.Remove(0, stringBuilder.Length);
			}
			if (vmdformat.ik_set_list != null)
			{
				stringBuilder.Append(vmdformat.ik_set_list.ik_set_count);
				stringBuilder.AppendLine();
				int num = 0;
				while ((long)num < (long)((ulong)vmdformat.ik_set_list.ik_set_count))
				{
					VMDFormat.IKSetData iksetData = vmdformat.ik_set_list.ik_set[num];
					stringBuilder.Append(iksetData.flame_no);
					stringBuilder.Append(": ");
					stringBuilder.Append(iksetData.ik_count);
					stringBuilder.Append('/');
					stringBuilder.Append(iksetData.display);
					stringBuilder.AppendLine();
					Dictionary<string, bool> ik_frame_map = iksetData.ik_frame_map;
					foreach (string text in ik_frame_map.Keys)
					{
						stringBuilder.AppendFormat("{0}:{1}", text, ik_frame_map[text]);
						stringBuilder.AppendLine();
					}
					num++;
				}
				string value5 = stringBuilder.ToString();
				using (TextWriter textWriter3 = File.CreateText(path + ".ik.txt"))
				{
					textWriter3.Write(value5);
				}
			}

			return vmdformat;
		}
	}
}
